package com.gm.commonlib.network.basenetwork.gson;
import com.gm.commonlib.network.basenetwork.numberformat.BooleanDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.DateDeserializer;
import com.gm.commonlib.network.basenetwork.numberformat.DateSerializer;
import com.gm.commonlib.network.basenetwork.numberformat.DoubleDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.FloatDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.IntegerDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.ListJsonDeserializerAdapter;
import com.gm.commonlib.network.basenetwork.numberformat.LongDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalDouble;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalDoubleDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalInteger;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalIntegerDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalLong;
import com.gm.commonlib.network.basenetwork.numberformat.OriginalLongDefault0Adapter;
import com.gm.commonlib.network.basenetwork.numberformat.StringDefault0Adapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.TypeAdapters;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

public class GsonUtil {
    public static Gson getFormatedGson() {
        final GsonBuilder builder = new GsonBuilder();
        builder.disableHtmlEscaping();
        builder.addSerializationExclusionStrategy(new IgnoreStrategy());
        builder.registerTypeAdapter(Date.class, new DateDeserializer()).setDateFormat(DateFormat.LONG);
        builder.registerTypeAdapter(Date.class, new DateSerializer()).setDateFormat(DateFormat.LONG);
        builder.registerTypeHierarchyAdapter(List.class, new ListJsonDeserializerAdapter());
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, new StringDefault0Adapter()));
        GsonBuilder gsonBuilder = builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalInteger.class, new OriginalIntegerDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalDouble.class, new OriginalDoubleDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(OriginalLong.class, new OriginalLongDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(long.class, Long.class, new LongDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, new IntegerDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(double.class, Double.class, new DoubleDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(float.class, Float.class, new FloatDefault0Adapter()));
        builder.registerTypeAdapterFactory(TypeAdapters.newFactory(boolean.class, Boolean.class, new BooleanDefault0Adapter()));
        return builder.create();
    }
}
